package com.lanou.bookstore.user.dao;

import com.lanou.Util.JDBCUtil;
import com.lanou.bookstore.user.domain.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

public class RegisterDao {
    //        public boolean register(User user) {
//        Connection conn = JDBCUtil.getConn();
//        String sqlInsert = "Insert into tb_user (uid,username,password)values(?,?,?)";
//        String uid = UUID.randomUUID().toString().replace("-", "");
//        try {
//            PreparedStatement psInsert = conn.prepareStatement(sqlInsert);
//            psInsert.setString(1, uid);
//            psInsert.setString(2, user.getUsername());
//            psInsert.setString(3, user.getPassword());
//            int row = psInsert.executeUpdate();
//            psInsert.close();
//            conn.close();
//            return row > 0;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//
//    public boolean hasThisName(String username) {
//        String sql = "select count(*) from tb_user where username=? ";
//        Connection conn = JDBCUtil.getConn();
//        try {
//            PreparedStatement ps = conn.prepareStatement(sql);
//            ps.setString(1, username);
//            ResultSet rs = ps.executeQuery();
//            rs.next();
//            int count = rs.getInt(1);
//            ps.close();
//            conn.close();
//            rs.close();
//            return count > 0;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
    public boolean register(User user) {
        Connection conn = JDBCUtil.getConn();
        String uid = UUID.randomUUID().toString().replace("-", "");
        String sqlInsert = "Insert into tb_user where (uid,username,password) values(?,?,?)";
        try {
            PreparedStatement psInsert = conn.prepareStatement(sqlInsert);
            psInsert.setString(1, uid);
            psInsert.setString(2, user.getUsername());
            psInsert.setString(3, user.getPassword());
            int row = psInsert.executeUpdate();
            psInsert.close();
            conn.close();
            return row > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean hasThisName(String username) {
        String sql = "select count(*) from tb_user where username=?";
        Connection conn = JDBCUtil.getConn();
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            rs.next();
            int count = rs.getInt(1);
            rs.close();
            ps.close();
            conn.close();
            return count > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
